90 - Versuche
06 - MipMap Kacheln
procedure TForm1.CreateScene;
const
w = 1.0;
begin
Shader := TShader.Create([FileToStr('Vertexshader.glsl'), FileToStr('Fragmentshader.glsl')]);
with Shader do begin
UseProgram;
Matrix_ID := UniformLocation('mat');
glUniform1i(UniformLocation('Sampler'), 0);
end;
Matrix := TMatrix.Create;
FrustumMatrix := TMatrix.Create;
FrustumMatrix.Frustum(-w, w, -w, w, 2.5, 1000.0);
// FrustumMatrix.Perspective(45, 1.0, 2.5, 1000.0); // Alternativ
WorldMatrix := TMatrix.Create;
WorldMatrix.Translate(0, 0, -200.0); // Die Scene in den sichtbaren Bereich verschieben.
WorldMatrix.Scale(5.0); // Und der Grösse anpassen.
WorldMatrix.RotateA(1.5); // Drehe um Y-Achse
WorldMatrix.RotateC(pi); // Drehe um Y-Achse
// RotMatrix := TMatrix.Create; // Die drei Konstruktoren
// ScaleMatrix := TMatrix.Create;
// prodMatrix := TMatrix.Create;
Texturen laden
procedure TForm1.InitScene;
const
maxLevel = 5;
var
tex: array of UInt32;
w, i, j: integer;
begin
// ------------ Texture laden --------------
glGenTextures(1, @textureID0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureID0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, maxLevel - 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
// glTexParameteri( GL_TEXTURE_3D, GL_GENERATE_MIPMAP, 1 );
for i := 1 to maxLevel do begin
w := (1 shl (maxLevel - i)) * 256 * 4;
// w := (1 shl i) * 256;
// WriteLn(w);
SetLength(tex, w * w);
for j := 0 to Length(tex) - 1 do begin
// tex[j] := $FF shl (4 * i) + (j mod 255);
//case i of
//1: tex[j] := $000FF;
//2: tex[j] := $00FF0;
//3: tex[j] := $0FF00;
//4: tex[j] := $FF000;
//end;
tex[j] := $FF00000 shr (i * 4);
end;
// WriteLn(IntToHex($FF00000 shr (i * 4), 8));
glTexImage2D(GL_TEXTURE_2D, i - 1, GL_RGBA, w, w, 0, GL_RGBA, GL_UNSIGNED_BYTE, Pointer(tex));
end;
glBindTexture(GL_TEXTURE_2D, 0);
glClearColor(0.6, 0.6, 0.4, 1.0); // Hintergrundfarbe
// Daten für Dreieck
glBindVertexArray(VBTriangle.VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBTriangle.VBOVertex);
glBufferData(GL_ARRAY_BUFFER, sizeof(Quad), @Quad, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, nil);
glBindBuffer(GL_ARRAY_BUFFER, VBTriangle.VBOTex);
glBufferData(GL_ARRAY_BUFFER, sizeof(TextureVertex), @TextureVertex, GL_STATIC_DRAW);
glEnableVertexAttribArray(10);
glVertexAttribPointer(10, 2, GL_FLOAT, False, 0, nil);
end;
procedure TForm1.ogcDrawScene(Sender: TObject);
var
x, y: integer;
const
d = 2.0;
s = 8;
begin
Matrix := TMatrix.Create;
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, textureID0);
Vertex-Shader:
Hier ist die Uniform-Variable mat hinzugekommen.
Diese wird im Vertex-Shader deklariert, Bewegungen kommen immer in diesen Shader.
#version 330
layout (location = 0) in vec3 inPos; // Vertex-Koordinaten
layout (location = 10) in vec2 inUV; // Textur-Koordinaten
uniform mat4 mat;
out vec2 UV0;
void main(void)
{
gl_Position = mat * vec4(inPos, 1.0);
UV0 = inUV;
}
Fragment-Shader:
#version 330
in vec2 UV0;
uniform sampler2D Sampler;
out vec4 FragColor;
void main()
{
FragColor = texture( Sampler, UV0, -5 );
// FragColor=vec4(1.0);
}